package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.server.entry.User;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * @
 */
public class AtAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, User> {

    private Context mContext;


    public AtAdapter(Context mContext, List<User> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_at, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, User data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getImage())) {
            BitmapLoader.ins().loadImage(data.getImage(),  holder.rivAvatar);
        }
        if (!TextUtils.isEmpty(data.getName())) {
            holder.tvUserName.setText(data.getName());
        }
        if (!TextUtils.isEmpty(data.getIntroduce())) {
            holder.tvUserDesc.setText(data.getIntroduce());
        }
        if (position == getDataList().size() - 1) {
            holder.vLine.setVisibility(View.GONE);
        } else {
            holder.vLine.setVisibility(View.VISIBLE);
        }
        holder.llFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.tvFollow.setSelected(!holder.tvFollow.isSelected());
                if (holder.tvFollow.isSelected()) {
                    holder.tvFollow.setText(mContext.getString(R.string.hasFollow));
                } else {
                    holder.tvFollow.setText(mContext.getString(R.string.unFollow));
                }
            }
        });
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvUserName,tvUserDesc;
        LinearLayout llFollow;
        TextView tvFollow;
        View vLine;

        public ViewHolder(View itemView) {
            super(itemView);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvUserDesc = (TextView) itemView.findViewById(R.id.tvUserDesc);
            llFollow = (LinearLayout) itemView.findViewById(R.id.llFollow);
            tvFollow = (TextView) itemView.findViewById(R.id.tvFollow);
            vLine = itemView.findViewById(R.id.vLine);
        }

    }

}
